<?php

namespace app\service;

use app\log\CountReqTime;
use app\log\UserLog;
use think\facade\Db;

class SqlListenService
{
    public function register()
    {
        // 服务注册
    }

    public function boot()
    {
        // 服务启动
        Db::listen(function ($sql, $runtime, $master) {
            //连接数据库-sql日志排除
            if (strpos($sql, "CONNECT") !== FALSE) {
                return;
            }

            // 进行监听处理
            $data = [
                'sql' => $sql,
                'runtime' => $runtime . "s",
                'master' => $master,
            ];

            CountReqTime::$execMysqlNum++;
            CountReqTime::$execMysqlTotalTime += $runtime;
            UserLog::Info("sql日志", $data);
        });
    }
}